<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 200px;
        height: 200px;
        border: 1px solid #000;
      }
    </style>
  </head>
  <body>
    <div>
      <img src="" style="width: 100%; height: 100%" alt="" />
    </div>
    <input type="file" />
    <script>
      document.querySelector('input').onchange = function (e) {
        const fd = new FormData()
        fd.append('avatar', e.target.files[0])

        // 发送ajax请求
        const xhr = new XMLHttpRequest()

        xhr.open('post', 'http://www.liulongbin.top:3006/api/upload/avatar')

        xhr.send(fd)

        // 获取结果
        xhr.onload = function () {
          const res = JSON.parse(xhr.responseText)
          console.log(res)
          document.querySelector('img').src =
            'http://www.liulongbin.top:3006' + res.url
        }
      }
    </script>
  </body>
</html>
